package dshop.action.admin;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.Product;
import dshop.model.company.Company;
import dshop.model.member.MemberGeneralInfo;

@Before({ ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class })
public class ProductAction extends ApiBaseController {
	public void list() {
		String keyWord = getPara("keyWord", "").trim();
		int auditStatus = getParaToInt("auditStatus", -1);
		// 排序
		String columnProp = getPara("columnProp", "").trim();
		String columnOrder = getPara("columnOrder", "").trim().split("ending")[0];
		String select = " select spi.product_id,sc.`name`,spi.product_name,DATE_FORMAT(spi.indate,'%Y-%m-%d') indate ,spi.audit_status ";
		String where = " from store_product_info spi LEFT JOIN store_company sc on spi.store_id = sc.store_id where 1=1  ";
		String orderBy = " order by spi.audit_status,spi.indate desc";
		List<Object> params = new ArrayList<Object>();
		if (!keyWord.isEmpty()) {
			where += " and ( spi.product_name like ? or sc.`name` like ?)";
			String keyWordParam = "%" + keyWord + "%";
			params.add(keyWordParam);
			params.add(keyWordParam);
		}
		if (auditStatus >= 0) {
			where += " and audit_status = ? ";
			params.add(auditStatus);
		}
		if (!columnOrder.isEmpty()) {
			orderBy = " order by " + columnProp + " " + columnOrder;
		}
		page(select, where + orderBy, params.toArray());
		renderSuccess();
	}

	public void detail() {
		int productId = getParaToInt("id");
		String select = " SELECT spi.product_id,spi.product_name,sct.`name` product_type,IF(spi.recommend = 0,'不推荐','推荐') recommend,IF(spi.put_away = 0,'下架','上架') put_away,"
				+ " spi.audit_status,DATE_FORMAT(spi.audit_date_time,'%Y-%m-%d') audit_date_time,spi.audit_status_explain,DATE_FORMAT(spi.indate,'%Y-%m-%d') indate, DATE_FORMAT(spi.modified_time,'%Y-%m-%d') modified_time,"
				+ " mpc.`name` product_mall_type,IF(spi.online_pay = 0,'线下支付','线上支付') online_pay,"
				+ " spi.sales,sppi.product_pic,sppi.product_detail_pic,st.admin_id ";
		String where = "FROM store_product_info spi "
				+ " LEFT JOIN store_product_pic_info sppi on spi.product_id =  sppi.product_id "
				+ " LEFT JOIN store st on st.id =  spi.store_id "
				+ " LEFT JOIN store_commodity_type sct on spi.type_id = sct.id "
				+ " LEFT JOIN mall_product_category mpc on spi.mall_type_id = mpc.id";
		where += " WHERE spi.product_id = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(productId);
		Record record = Db.findFirst(select + where, params.toArray());

		Integer accountId = record.getInt("admin_id");
		record.set("account", MemberGeneralInfo.dao.findById(accountId));
		record.set("company", Company.getCompanyInfoByAccountId(accountId));
		
		record.set("product_pic", JSON.parseArray(record.getStr("product_pic"),String.class));
		record.set("product_detail_pic", JSON.parseArray(record.getStr("product_detail_pic"),String.class));
		List<Record> product_spec = Db.find("SELECT * FROM store_commodity_specification WHERE product_id = ?",params.toArray());
		for (Record item : product_spec) {
			List<String> specification_pics = new ArrayList<String>();
			specification_pics.add(item.getStr("specification_pic"));
			item.set("specification_pic", specification_pics);
		}
		record.set("product_spec", product_spec);
		setAttr("product", record);
		renderSuccess();
	}

	public void checkStatusPass() {
		int productId = getParaToInt("product_id");
		String remark = getPara("remark", "").trim();
		Product.setCheckStatus(productId, 1, remark);

		renderSuccess("成功设置为已通过");
	}

	public void checkStatusFail() {
		int productId = getParaToInt("product_id");
		String remark = getPara("remark", "").trim();
		Product.setCheckStatus(productId, 2, remark);

		renderSuccess("已设置为不通过");
	}
}
